﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
#if LIVE
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;
#endif
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
#if WINDOWS

using Microsoft.Xna.Framework.Storage;
#endif
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using Neat.GUI;
using Neat.Mathematics;
using Neat.Graphics;

namespace RiverRaidX
{
    public class GameOverScreen : Screen
    {
        float alpha;
        float fadeRate = 0.02f;
        int score = 0;
        public GameOverScreen(NeatGame Game)
            : base(Game)
        {
        }

        Label l1;
        TypeWriter l2;
        Label l3;
        public override void Initialize()
        {
            base.Initialize();

            l1 = (Label)Form.NewControl("l1", new Label());
            l1.Font = "menufont";
            l2 = (TypeWriter)Form.NewControl("l2", new TypeWriter());
            l2.Font = "menufont";
            l3 = (Label)Form.NewControl("l3", new Label());
            l3.Font = "menufont";
            l3.ForeColor = Color.OrangeRed;
            l3.Caption = "Press Start";
        }

        RenderTarget2D target;
        public override void LoadContent()
        {
            base.LoadContent();
            target = new RenderTarget2D(game.GraphicsDevice, game.GameWidth, game.GameHeight, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
        }

        public override void Activate()
        {
            alpha = -.2f;
            score = game.Console.Ram["score"].ReturnInt();
            base.Activate();

            l1.Caption = "Game Over!";
            l1.Center();
            
            l1.Position -= new Vector2(0, 50);

            l2.Caption = "Score: " + score;
            l2.Center();
            l2.Position += new Vector2(0, 50);
            l2.Reset();

            l3.Center();
            l3.Position = new Vector2(l3.Position.X, game.GameHeight - 100);
        }

        public override void Deactivate(string nextScreen)
        {
            target = new RenderTarget2D(game.GraphicsDevice, game.GameWidth, game.GameHeight, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);
            base.Deactivate(nextScreen);
        }

        TimeSpan lastSpan;
        bool flip = false;
        public override void Behave(GameTime gameTime)
        {
            if (alpha < 1)
                alpha += fadeRate;
            
            if ((gameTime.TotalGameTime - lastSpan).TotalSeconds > 1)
            {
                lastSpan = gameTime.TotalGameTime;
                flip = !flip;
                l3.ForeColor = (flip ? Color.Transparent : Color.White);
            }

            base.Behave(gameTime);
        }

        public override void HandleInput(GameTime gameTime)
        {
            if (game.IsTapped(Keys.Space) || game.IsTapped(Keys.Enter) || game.IsTapped(Keys.Escape)
                || game.IsTapped(Buttons.Start))
                game.Console.Run("sh mainmenu");
#if KINECT
            foreach (var item in game.Touch.TrackPoints)
            {
                if (item.Hold) game.Console.Run("sh mainmenu");
            }
#endif
            base.HandleInput(gameTime);
        }

        public override void Render(GameTime gameTime)
        {
            game.GraphicsDevice.Clear(Color.Black);
            game.SpriteBatch.End();
            game.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
            if (alpha < 1)
            {
                game.Screens["game"].Render(gameTime);
                game.SpriteBatch.Draw(game.GetTexture("solid"), new Rectangle(0, 0, game.GameWidth, game.GameHeight), GraphicsHelper.GetColorWithAlpha(Color.Black, alpha));
            }
            game.SpriteBatch.End();

            game.GraphicsDevice.SetRenderTarget(target);
            game.SpriteBatch.Begin();
            if (alpha >= 0) game.GraphicsDevice.Clear(Color.Black);
            base.Render(gameTime);
            game.SpriteBatch.End();
            game.GraphicsDevice.SetRenderTarget(game.DefaultTarget);

            var fsfx = game.GetEffect("fullscreen");
            fsfx.Parameters["noise"].SetValue((float)game.RandomGenerator.NextDouble());
            game.SpriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, DepthStencilState.None, null, fsfx) ;
            game.SpriteBatch.Draw(target, Vector2.Zero, GraphicsHelper.GetColorWithAlpha(Color.White, alpha));

            game.RestartBatch();
            l3.Draw(gameTime, SpriteBatch);
        }
    }
}
